package 力扣;

import org.ietf.jgss.Oid;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author yyq
 * @create 2022-04-07 10:50
 */
public class leetcode104 {

    public static void main(String[] args) {
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        System.out.println(maxDep(root));
    }
    public static void add(Integer a){
        a=a+1;

    }

    public static int maxDep(TreeNode root){
        int[] max=new int[1];
        max[0]=0;
        int temp=0;
        if(root==null) return max[0];
        preOrder(temp,max,root);
        return max[0];
    }

    private static int preOrder(int temp, int[] max, TreeNode root) {
        temp++;
        if (temp>max[0]) max[0]=temp;
        if(root.left!=null) {
            temp = preOrder(temp,max,root.left);
            temp--;
        }

        if(root.right!=null){
            temp = preOrder(temp,max,root.right);
            temp--;
        }
        return temp;

    }

    public int maxDepth(TreeNode root) {
        int max = 0;
        int LayerNodes = 1;
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null) return max;
        queue.add(root);

        while (!queue.isEmpty()){
            int temp = 0;
            while (LayerNodes!=0){
                TreeNode treeNode = queue.poll();

                if(treeNode.left!=null)
                {
                    queue.add(treeNode.left);
                    temp++;
                }
                if(treeNode.right!=null)
                {
                    queue.add(treeNode.right);
                    temp++;
                }
                LayerNodes--;

            }
            LayerNodes=temp;
            max++;
        }
        return max;
    }
}
